package com.tanhua.server.controller;

import com.tanhua.domain.db.User;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 通过手机号码查询
     * ResponseEntity 由spring封装的响应http协议的结果，返回给客户端
     * ResponseEntity.ok(数据);  ok代表状态码为200
     * ResponseEntity.ok(null);
     * ResponseEntity.status(状态码) 500 后台出错了
     */
    @GetMapping("/findByMobile")
    public ResponseEntity findByMobile(String phone){
        // 调用业务 查询
        User user = userService.findByMobile(phone);
        return ResponseEntity.ok(user);
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping
    public ResponseEntity add(@RequestBody User user){
        userService.add(user);
        return ResponseEntity.ok(null);
    }

    /**
     * 登录注册 - 发送验证码
     * @param paramMap
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity sendValidateCode(@RequestBody Map<String,String> paramMap){
        userService.sendValidateCode(paramMap);
        return ResponseEntity.ok(null);
    }

    /**
     * 登录注册 - 验证码校验
     * @param paramMap
     * @return
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String,String> paramMap){
        // 验证码校验，通过后，签发token认证令牌
        Map<String,Object> resultMap = userService.loginVerification(paramMap);
        return ResponseEntity.ok(resultMap);
    }
}
